<template>
  <el-dialog
    ref="refDlg"
    v-el-drag-dialog
    :title="`${(detailData && detailData.id) ? '编辑' : '新增'}类目`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="600px"
    append-to-body
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <!-- <el-col :span="24">
            <el-form-item label="父级类目:">
              {{ (parentCate && parentCate.id) ? parentCate.name : '一级目录' }}
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="类目名称:" prop="categoryName">
              <el-input v-model.trim="formData.categoryName" maxlength="20" placeholder="请输入类目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否启用:" prop="state">
              <el-switch
                v-model="formData.state"
                active-color="#13ce66"
                inactive-color="#999999"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类目图标:">
              <div class="img-upload-container">
                <qup-img-uploader
                  :width="100"
                  :height="80"
                  mask
                  :image-url="cateImage.absoluteUrl"
                  :relative-url="cateImage.relativeUrl"
                  @upload-success="uploadSuccess"
                  @remove="deleteImage"
                />
                <span class="icon-msg">建议图片大小100*100,支持png\jpg\jpeg</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button v-preventReClick v-loading="submiting" size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveCategory } from '@/api/category'
import QupImgUploader from '@/components/imgUploader'
export default {
  name: 'HelpCategoryEdit',
  components: { QupImgUploader },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    },
    parentCate: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        categoryName: '',
        state: 1,
        iconUrl: ''
      },
      formRules: {
        categoryName: [
          { required: true, message: '请输入类目名称', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] }
        ]
      },
      cateImage: {
        absoluteUrl: '',
        relativeUrl: ''
      }
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    const data = this.detailData
    if (data) {
      this.formData = {
        state: data.state,
        categoryName: data.categoryName,
        imageUrl: data.iconUrl
      }
      this.cateImage = {
        absoluteUrl: this.formatImageUrl(data.iconUrl),
        relativeUrl: data.iconUrl
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        categoryName: '',
        state: 1,
        imageUrl: ''
      }
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          const tmp = {}
          if (this.parentCate) {
            const { id, classifyCode } = this.parentCate
            tmp.classifyCode = classifyCode
            if (id) {
              tmp.parentId = id
            }
          }
          if (this.detailData.id) {
            tmp.id = this.detailData.id
          }
          const params = { ...this.formData, ...tmp }
          this.submiting = true
          saveCategory(params).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: res.msg || '保存成功',
                type: 'success'
              })
              this.$emit('success', !params.id)
              this.closeDialog()
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    },
    uploadSuccess(response, file, fileList) {
      this.formData.iconUrl = response.fileName
      this.cateImage = {
        absoluteUrl: response.sourceFileUrl,
        relativeUrl: response.fileName
      }
    },
    deleteImage() {
      this.formData.iconUrl = ''
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
  .img-upload-container {
    width: fit-content;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    align-items: center;
    .icon-msg{
      margin-left: 10px;
      color: #999;
    }
  }
</style>
